/*------------------------------------------------------------------------------

Project: Wouter Schakel and Armen Hakhverdian (2018). Ideological Congruence
	and Socio-Economic Inequality. European Political Science Review, 10(3):
	441-465. doi: 10.1017/S1755773918000036

Data: Dutch Parliamentary Election Studies of 2006 and 1971-2006, downloaded from
	https://easy.dans.knaw.nl/ui/datasets/id/easy-dataset:45027 (2006)
	https://easy.dans.knaw.nl/ui/datasets/id/easy-dataset:34143 (1971-2006)

	Dutch Parliament Studies of 2006, 2001 and 1990, obtained from Rudy Andeweg
	(not publicly available!)
	
Description: This do-file contains the commands to produce figures 1-5, 8, 9,
E1, E2 and I1 in Schakel and Hakhverdian (2018):

	1. Many-to-many congruence for all citizens on income differences
	2a-b. Many-to-many congruence for income groups on income differences
	3a-b. Many-to-many congruence for education groups on multiculturalism
	4. Congruence on all issues by income
	5. Congruence on all issues by education
	8a-d. Developments in congruence over time for income groups
	9a-d. Developments in congruence over time for education groups
	E1. Congruence on left-right and issues by income
	E2. Congruence on left-right and issues by education
	I1. Scatterplot of frequency and cumulative congruence

------------------------------------------------------------------------------*/

* Note: this do-file uses two datasets ("Frequencies" and "Bootstrap output")
* that were produced with the data preparation and analysis do-file.
global data "YOUR PATH HERE"
global graphs "YOUR PATH HERE"

ssc install drarea

* To produce figures 1 to 3, run the following loops (all at once):
forval x=1/42 {
	forval y=1/2 {
	use "$data\Frequencies.dta", clear
	rename v`x'_`y' scale
	quietly table scale, c(freq) replace name(bs`x'`y')
	egen tot`x'`y' = total(bs`x'`y')
	quietly gen rf`x'`y' = bs`x'`y'/tot`x'`y'*100
	drop bs`x'`y' tot`x'`y'
	tempfile small`x'`y'
	quietly save "`small`x'`y''", replace
	}
	}

clear
set obs 7
gen scale=_n
gen low=0

forval x=1/42 {
	forval y=1/2 {
	quietly merge 1:1 scale using "`small`x'`y''", nogen
	}
	}

/*------------------------------------------------------------------------------
 1. Many-to-many congruence for all citizens on income differences
------------------------------------------------------------------------------*/

drarea rf12 low rf11 low scale,												///
	xt("") xsize(7) color("gs16" "gs16" "gs14")								///
	xlab(1(1)7	1 `""1" "Less equal""' 7 `""7" "More equal""')				///
	yt("Percentage", size(medlarge)) ylab(0(10)30, angle(hor) nogrid)		///
	legend(order(4 "Representatives" 5 "Citizens") symx(10) reg(lc(white)))	///
	text(27 1.9 "78.5% overlap", size(medlarge))							///
	graphr(m(5 12 0 5) c(white)) plotr(m(zero)) scale(1.1)					///
	twoway(line rf12 scale, lc(black) ||									///
	line rf11 scale, lc(black) lp(dash) )

/*------------------------------------------------------------------------------
 2a-b. Many-to-many congruence for income groups on income differences
------------------------------------------------------------------------------*/

drarea rf22 low rf21 low scale,												///
	title("(a) Low incomes", size(large) c(black) m(esubhead)) 				///
	xt("") xsize(7) color("gs16" "gs16" "gs14")								///
	xlab(1(1)7	1 `""1" "Less equal""' 7 `""7" "More equal""')				///
	yt("Percentage", size(medlarge)) ylab(0(10)40, angle(hor) nogrid)		///
	legend(order(4 "Representatives" 5 "Citizens") symx(10) reg(lc(white)))	///
	text(35 1.9 "69.1% overlap", size(medlarge))							///
	graphr(m(5 12 0 5) c(white)) plotr(m(zero))								///
	twoway(line rf22 scale, lc(black) ||									///
	line rf21 scale, lc(black) lp(dash) )

graph save Graph "$graphs\2a.gph"

drarea rf42 low rf41 low scale,												///
	title("(b) High incomes", size(large) c(black) m(esubhead))				///
	xt("") xsize(7) color("gs16" "gs16" "gs14")								///
	xlab(1(1)7	1 `""1" "Less equal""' 7 `""7" "More equal""')				///
	yt("Percentage", size(medlarge)) ylab(0(10)40, angle(hor) nogrid)		///
	legend(order(4 "Representatives" 5 "Citizens") symx(10) reg(lc(white)))	///
	text(35 1.9 "88.8% overlap", size(medlarge))							///
	graphr(m(5 12 0 5) c(white)) plotr(m(zero))								///
	twoway(line rf42 scale, lc(black) ||									///
	line rf41 scale, lc(black) lp(dash) )

graph save Graph "$graphs\2b.gph"

/* Save the graphs after making the changes specified above.
The grc1leg command is used to produce a table of graphs, but this seems to
contain a glitch, causing it to ignore size-commands. Hence, you have to set the
X-size (8) and Y-size (10) manually. */
grc1leg "$graphs\2a.gph" "$graphs\2b.gph",									///
	col(1) graphregion(margin(vsmall) color(gs16))

rm "$graphs\2a.gph"
rm "$graphs\2b.gph"

/*------------------------------------------------------------------------------
 3a-b. Many-to-many congruence for education groups on multiculturalism
------------------------------------------------------------------------------*/

drarea rf192 low rf191 low scale,											///
	title("(a) Low education", size(large) c(black) m(esubhead))			///
	xt("") xsize(7) color("gs16" "gs16" "gs14")								///
	xlab(1(1)7	1 `""1" "Multicultural""' 7 `""7" "Monocultural""')			///
	yt("Percentage", size(medlarge)) ylab(0(10)50, angle(hor) nogrid)		///
	legend(order(4 "Representatives" 5 "Citizens") symx(10) reg(lc(white)))	///
	text(45 1.9 "53.0% overlap", size(medlarge))							///
	graphr(m(5 12 0 5) c(white)) plotr(m(zero))								///
	twoway(line rf192 scale, lc(black) ||									///
	line rf191 scale, lc(black) lp(dash) )

graph save Graph "$graphs\3a.gph"

drarea rf212 low rf211 low scale,											///
	title("(b) High education", size(large) c(black) m(esubhead))			///
	xt("") xsize(7) color("gs16" "gs16" "gs14")								///
	xlab(1(1)7	1 `""1" "Multicultural""' 7 `""7" "Monocultural""')			///
	yt("Percentage", size(medlarge)) ylab(0(10)50, angle(hor) nogrid)		///
	legend(order(4 "Representatives" 5 "Citizens") symx(10) reg(lc(white)))	///
	text(45 1.9 "94.1% overlap", size(medlarge))							///
	graphr(m(5 12 0 5) c(white)) plotr(m(zero))								///
	twoway(line rf212 scale, lc(black) ||									///
	line rf211 scale, lc(black) lp(dash) )

graph save Graph "$graphs\3b.gph"

* Set X-size to 8 and Y-size to 10
grc1leg "$graphs\3a.gph" "$graphs\3b.gph",									///
	col(1) graphregion(margin(vsmall) color(gs16))

rm "$graphs\3a.gph"
rm "$graphs\3b.gph"

/*------------------------------------------------------------------------------
 To produce all figures below (4-I1), run the following commands (all at once):
------------------------------------------------------------------------------*/

use "$data\Bootstrap output.dta", clear
gen n=_n
quietly tabstat congruence1-congruence91, stat(sd) save
matrix sd=r(StatTotal)'
svmat sd
drop congruence*
keep in 1/91
tempfile sd
save "`sd'", replace

forval x=1/91 {
	forval y=1/2 {
		use "$data\Frequencies.dta", clear
		rename v`x'_`y' scale
		bys scale, sort: gen f`x'_`y' = _N
		bys scale: gen cf`x'_`y' = _N if _n == 1
		replace cf`x'_`y'=sum(cf`x'_`y')
		egen pickone=tag(scale)
		keep if pickone==1
		drop pickone
		tempfile small`x'_`y'
		quietly save "`small`x'_`y''", replace
		}
	}

clear
set obs 7
gen scale = _n

forval x=1/91 {
	forval y=1/2 {
		quietly merge 1:1 scale using "`small`x'_`y''", nogen
		egen tot`x'_`y' = total(f`x'_`y')
		quietly gen rf`x'_`y'=f`x'_`y'/tot`x'_`y'*100
		quietly gen rcf`x'_`y'=cf`x'_`y'/tot`x'_`y'*100
		quietly recode rf`x'_`y' (.=0) in 1/7
		drop tot`x'_`y' f`x'_`y' cf`x'_`y'
		}
		
	egen overlap`x'=rowmin(rf`x'_1 rf`x'_2)
	gen diff`x'=abs(rcf`x'_1-rcf`x'_2)
	egen congruence`x'=total(overlap`x')
	egen cumulative`x'=total(diff`x')
	keep in 1
	drop overlap`x' diff`x' rf`x'_1 rf`x'_2 rcf`x'_1 rcf`x'_2
	}

drop v*
reshape long congruence cumulative, i(scale) j(n)
drop scale
merge 1:1 n using "`sd'", nogen

gen lowci=congruence-(sd1*1.96)
gen highci=congruence+(sd1*1.96)

/*------------------------------------------------------------------------------
 4. Congruence on all issues by income
------------------------------------------------------------------------------*/

recode n (2=1) (3=2) (4=3) (9=5) (10=6) (11=7) (16=9) (17=10) (18=11)		///
	(30=13) (31=14) (32=15) (23=17) (24=18) (25=19) (else=.), gen(incissue)

recode incissue (5=1) (6=2) (7=3) (9=1) (10=2) (11=3) (13=1) (14=2) (15=3)	///
	(17=1) (18=2) (19=3), gen(inc)

egen issue=seq(), from(1) to(7) block(7)
recode issue (5=4) (4=5)
	
twoway (bar congruence incissue if inc==1, fc(gs6) fi(100) lc(gs6))			///
	(bar congruence incissue if inc==2, fc(gs13) fi(100) lc(gs13))			///
	(bar congruence incissue if inc==3, fc(gs9) fi(100) lc(gs9))			///
	(rcap lowci highci incissue, lc(black) msize(zero)),					///
	xt("") yt("Congruence (%)", size(medium))								///
	xlab(2(4)18 2 `""Income" "differences""' 6 `""European" "integration""'	///
	10 `""Multicul-" "turalism""'14 "Euthanasia"							///
	18 "Crime", labs(medsmall) labgap(tiny) notick)							///
	legend(order(1 "Low" 2 "Middle" 3 "High")								///
	bm(medium) symx(8) c(1) pos(3) size(medsmall) reg(lc(white))			///
	title("Income", size(medium) bex c(black) j(left)))						///
	plotr(m(4 4 0 0) lc(white)) graphr(m(2 0 2 2) fc(white) lc(white))		///
	ylab(0(20)100, angle(hor)) yline(20(20)100, lc(gs15)) xsize(6.7) ysize(4)

/*------------------------------------------------------------------------------
 5. Congruence on all issues by education
------------------------------------------------------------------------------*/

recode n (5=1) (6=2) (7=3) (12=5) (13=6) (14=7) (19=9) (20=10) (21=11)		///
	(33=13) (34=14) (35=15) (26=17) (27=18) (28=19) (else=.), gen(eduissue)

recode eduissue (5=1) (6=2) (7=3) (9=1) (10=2) (11=3) (13=1) (14=2) (15=3)	///
	(17=1) (18=2) (19=3), gen(edu)

twoway (bar congruence eduissue if edu==1, fc(gs6) fi(100) lc(gs6))			///
	(bar congruence eduissue if edu==2, fc(gs13) fi(100) lc(gs13))			///
	(bar congruence eduissue if edu==3, fc(gs9) fi(100) lc(gs9))			///
	(rcap lowci highci eduissue, lc(black) msize(zero)),					///
	xt("") yt("Congruence (%)", size(medium))								///
	xlab(2(4)18 2 `""Income" "differences""' 6 `""European" "integration""'	///
	10 `""Multicul-" "turalism""'14 "Euthanasia"							///
	18 "Crime", labs(medsmall) labgap(tiny) notick)							///
	legend(order(1 "Low" 2 "Middle" 3 "High")								///
	bm(medium) symx(8) c(1) pos(3) size(medsmall) reg(lc(white))			///
	title("Education", size(medium) bex c(black) j(left)))					///
	plotr(m(4 4 0 0) lc(white)) graphr(m(2 0 2 2) fc(white) lc(white))		///
	ylab(0(20)100, angle(hor)) yline(20(20)100, lc(gs15)) xsize(6.7) ysize(4)

drop inc* *issue edu

/*------------------------------------------------------------------------------
 8a-d. Developments in congruence over time for income groups
------------------------------------------------------------------------------*/

gen year=3.01
replace year=2.01 if n>=43 & n<=77
replace year=1.01 if n>=78
gen year2=year-0.02

drop n
egen n=seq(), from(1) to(91)

replace n=n-42 if n>=43 & n<=63
replace n=n-35 if n>=64 & n<=77
replace n=n-77 if n>=78 & n<=84
replace n=n-56 if n>=85 & n<=91

reshape wide cumulative congruence sd1 lowci highci, i(year) j(n)

twoway (line congruence2 year2, lc(black))									///
	(line congruence4 year, lc(black) lp(dash))								///
	(rspike lowci2 highci2 year2, lc(black))								///
	(rspike lowci4 highci4 year, lc(black) lp(shortdash)),					///
	title("(a) Income differences", size(large) c(black) m(esubhead))		///
	xt("") yt("Congruence", size(large))									///
	xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")						///
	ylab(50(10)100, angle(hor)) yline(50 60 70 80 90 100, lc(gs15))			///
	legend(order(1 "Low incomes" 2 "High incomes") symx(10) reg(lc(white)))	///
	graphr(c(white))

graph save Graph "$graphs\8a.gph", replace

twoway (line congruence9 year2, lc(black))									///
	(line congruence11 year, lc(black) lp(dash))							///
	(rspike lowci9 highci9 year2, lc(black))								///
	(rspike lowci11 highci11 year, lc(black) lp(shortdash)),				///
	title("(b) European integration", size(large) c(black) m(esubhead))		///
	xt("") xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")					///
	ylab(50(10)100, angle(hor)) yline(50 60 70 80 90 100, lc(gs15))			///
	legend(order(1 "Low incomes" 2 "High incomes") symx(10) reg(lc(white)))	///
	graphr(c(white))

graph save Graph "$graphs\8b.gph", replace
	
twoway (line congruence16 year2, lc(black))									///
	(line congruence18 year, lc(black) lp(dash))							///
	(rspike lowci16 highci16 year2, lc(black))								///
	(rspike lowci18 highci18 year, lc(black) lp(shortdash)),				///
	title("(c) Multiculturalism", size(large) c(black) m(esubhead))			///
	xt("") yt("Congruence", size(large))									///
	xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")						///
	ylab(50(10)100, angle(hor)) yline(50 60 70 80 90 100, lc(gs15))			///
	legend(order(1 "Low incomes" 2 "High incomes") symx(10) reg(lc(white)))	///
	graphr(c(white))

graph save Graph "$graphs\8c.gph", replace
	
twoway (line congruence30 year2, lc(black))									///
	(line congruence32 year, lc(black) lp(dash))							///
	(rspike lowci30 highci30 year2, lc(black))								///
	(rspike lowci32 highci32 year, lc(black) lp(shortdash)),				///
	title("(d) Euthanasia", size(large) c(black) m(esubhead))				///
	xt("") xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")					///
	ylab(50(10)100, angle(hor)) yline(50 60 70 80 90 100, lc(gs15))			///
	legend(order(1 "Low incomes" 2 "High incomes") symx(10) reg(lc(white)))	///
	graphr(c(white))

graph save Graph "$graphs\8d.gph", replace

grc1leg "$graphs\8a.gph" "$graphs\8b.gph" "$graphs\8c.gph" "$graphs\8d.gph", ///
	legendfrom("$graphs\8a.gph") graphregion(margin(vsmall) color(gs16))

graph save Graph "$graphs\8.gph", replace

/*------------------------------------------------------------------------------
 9a-d. Developments in congruence over time for education groups
------------------------------------------------------------------------------*/

* If you haven't already, run the commands that precede figure 8

twoway (line congruence5 year2, lc(black))									///
	(line congruence7 year, lc(black) lp(dash))								///
	(rspike lowci5 highci5 year2, lc(black))								///
	(rspike lowci7 highci7 year, lc(black) lp(shortdash)),					///
	title("(a) Income differences", size(large) c(black) m(esubhead))		///
	xt("") yt("Congruence", size(large))									///
	xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")						///
	ylab(40(15)100, angle(hor)) yline(40 55 70 85 100, lc(gs15))			///
	legend(order(1 "Low education" 2 "High education") symx(10)				///
	reg(lc(white))) graphr(c(white))

graph save Graph "$graphs\9a.gph", replace
	
twoway (line congruence12 year2, lc(black))									///
	(line congruence14 year, lc(black) lp(dash))							///
	(rspike lowci12 highci12 year2, lc(black))								///
	(rspike lowci14 highci14 year, lc(black) lp(shortdash)),				///
	title("(b) European integration", size(large) c(black) m(esubhead))		///
	xt("") xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")					///
	ylab(40(15)100, angle(hor)) yline(40 55 70 85 100, lc(gs15))			///
	legend(order(1 "Low education" 2 "High education") symx(10)				///
	reg(lc(white))) graphr(c(white))

graph save Graph "$graphs\9b.gph", replace
	
twoway (line congruence19 year2, lc(black))									///
	(line congruence21 year, lc(black) lp(dash))							///
	(rspike lowci19 highci19 year2, lc(black))								///
	(rspike lowci21 highci21 year, lc(black) lp(shortdash)),				///
	title("(c) Multiculturalism", size(large) c(black) m(esubhead))			///
	xt("") yt("Congruence", size(large))									///
	xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")						///
	ylab(40(15)100, angle(hor)) yline(40 55 70 85 100, lc(gs15))			///
	legend(order(1 "Low education" 2 "High education") symx(10)				///
	reg(lc(white))) graphr(c(white))

graph save Graph "$graphs\9c.gph", replace

twoway (line congruence33 year2, lc(black))									///
	(line congruence35 year, lc(black) lp(dash))							///
	(rspike lowci33 highci33 year2, lc(black))								///
	(rspike lowci35 highci35 year, lc(black) lp(shortdash)),				///
	title("(d) Euthanasia", size(large) c(black) m(esubhead))				///
	xt("") xlab(1(1)3 1 "1989/1990" 2 "1998/2001" 3 "2006")					///
	ylab(40(15)100, angle(hor)) yline(40 55 70 85 100, lc(gs15))			///
	legend(order(1 "Low education" 2 "High education") symx(10)				///
	reg(lc(white))) graphr(c(white))

graph save Graph "$graphs\9d.gph", replace

grc1leg "$graphs\9a.gph" "$graphs\9b.gph" "$graphs\9c.gph" "$graphs\9d.gph", ///
	legendfrom("$graphs\9a.gph") graphregion(margin(vsmall) color(gs16))

graph save Graph "$graphs\9.gph", replace

* Removing the subgraphs of figures 8 and 9
rm "$graphs\8a.gph"
rm "$graphs\8b.gph"
rm "$graphs\8c.gph"
rm "$graphs\8d.gph"
rm "$graphs\9a.gph"
rm "$graphs\9b.gph"
rm "$graphs\9c.gph"
rm "$graphs\9d.gph"

/*------------------------------------------------------------------------------
 E1. Congruence on left-right and issues by income
------------------------------------------------------------------------------*/

drop if n>=43
egen group=seq(), from(1) to(7) block(1)
recode n (1/35=0) (36/42=1), gen(leftright)
recode n (2=1) (3=2) (4=3) (5=9) (6=10) (7=11) (37=5) (38=6) (39=7) (40=13)	///
	(41=14) (42=15) (else=.), gen(x)

egen avcong = mean(congruence), by(group leftright)
egen avsd = mean(sd1), by(group leftright)
replace avsd = avsd / sqrt(5)
gen avlowci=avcong-(avsd*1.96)
gen avhighci=avcong+(avsd*1.96)
replace avlowci=lowci if leftright==1
replace avhighci=highci if leftright==1

twoway (bar avcong x if group==2, fc(gs6) fi(100) lc(gs6))					///
	(bar avcong x if group==3, fc(gs13) fi(100) lc(gs13))					///
	(bar avcong x if group==4, fc(gs9) fi(100) lc(gs9))						///
	(rcap avlowci avhighci x if group<5, lc(black) msize(zero)),			///
	xt("") yt("Congruence (%)", size(medium))								///
	xlab(2(4)6 2 "Average" 6 "Left-right", labs(medium) labgap(tiny)		///
	notick) legend(order(1 "Low" 2 "Middle" 3 "High")						///
	bm(1 0 0 0) symx(8) c(1) pos(3) size(medsmall) reg(lc(white))			///
	title("Income", size(medium) bex c(black) j(left)))						///
	plotr(m(3 3 0 2) lc(white)) graphr(m(2 0 2 2) fc(white) lc(white))		///
	ylab(0(20)100, angle(hor)) yline(20(20)100, lc(gs15)) xsize(5) ysize(4)

/*------------------------------------------------------------------------------
 E2. Congruence on left-right and issues by education
------------------------------------------------------------------------------*/

* If you haven't already, run the commands that precede figure E1

twoway (bar avcong x if group==5, fc(gs6) fi(100) lc(gs6))					///
	(bar avcong x if group==6, fc(gs13) fi(100) lc(gs13))					///
	(bar avcong x if group==7, fc(gs9) fi(100) lc(gs9))						///
	(rcap avlowci avhighci x if group>4, lc(black) msize(zero)),			///
	xt("") yt("Congruence (%)", size(medium))								///
	xlab(10(4)14 10 "Average" 14 "Left-right", labs(medium)					///
	labgap(tiny) notick) legend(order(1 "Low" 2 "Middle" 3 "High")			///
	bm(2 3 0 0) symx(8) c(1) pos(3) size(medsmall) reg(lc(white))			///
	title("Education", size(medium) bex c(black) j(left)))					///
	plotr(m(3 3 0 2) lc(white)) graphr(m(2 0 2 2) fc(white) lc(white))		///
	ylab(0(20)100, angle(hor)) yline(20(20)100, lc(gs15)) xsize(5) ysize(4)

/*------------------------------------------------------------------------------
 I1. Scatterplot of frequency and cumulative congruence
------------------------------------------------------------------------------*/

scatter congruence cumulative,												///
	xt("Congruence (cumulative)", size(medium) m(0 0 0 2))					///
	yt("Congruence (frequency)", size(medium) m(0 2 0 0))					///
	mc(gs0) xlab(0(40)160) ylab(0(20)100, nogrid) graphr(m(2 3 2 3) c(white))
